﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Forget.HRBLL.Logical;
using Forget.HRBLL.View;


namespace HRWebSite
{
    public class BasePage : System.Web.UI.Page
    {
        protected Forget.HRBLL.Model.User HRUser;
        protected Limits HRLimits;
        protected Forget.HRBLL.LimitAttribute HRLimitAttribute;

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
                Page.Response.Redirect(string.Format(@"~\user\login.aspx?url={0}", Server.UrlEncode(this.Request.Url.AbsoluteUri)));
            else
            {
                HRUser = (Forget.HRBLL.Model.User)Session["CurUser"];
                if (this.HRLimitAttribute != Forget.HRBLL.LimitAttribute.None)
                {
                    if (!UserLimitLogical.GetUserLimit(this.HRUser.NumberID, this.HRLimits.ToString(), this.HRLimitAttribute))
                    {
                        Response.Write("<script>alert('十分抱歉，您没有权限查看本页！');location='../default.aspx'</script>");
                        Response.End();
                    }
                }
            }
        }
    }

    public class BaseMasterPage : System.Web.UI.MasterPage
    {
        protected Forget.HRBLL.Model.User HRUser;        
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] != null)
                HRUser = (Forget.HRBLL.Model.User)Session["CurUser"];
        }
    }

    /// <summary>
    /// 这个类需要注意属性必须按大小顺序写否则影响整体
    /// </summary>
    public class InnerLimint
    {
        public string EnID { get; set; }
        public bool Read { get; set; }
        public bool Add { get; set; }
        public bool Delete { get; set; }
        public bool Modify { get; set; }
        public bool Audit { get; set; }

        public static List<InnerLimint> FromLimitView(List<LimitView> views)
        {
            List<InnerLimint> retValue = new List<InnerLimint>();
            foreach (var v in views)
            {
                InnerLimint il = new InnerLimint();
                il.EnID = v.EnID;
                if (v.Attributes != null)
                {
                    il.Read = (v.Attributes & 1) == 1 ? true : false;
                    il.Add = (v.Attributes & 2) == 2 ? true : false;
                    il.Delete = (v.Attributes & 4) == 4 ? true : false;
                    il.Modify = (v.Attributes & 8) == 8 ? true : false;
                    il.Audit = (v.Attributes & 16) == 16 ? true : false;
                }
                retValue.Add(il);
            }
            return retValue;
        }
    }

    public enum Limits
    {
        UserInfo,
        UserManage,
        LimitManage,
        HolidyAudit,
        DictManage,
        EmployeeManage,
        DepartmentManage
    }
}